angular.module('Controller')
.controller('WFAccountManageNewCtrl', ($scope,NgTableParams,$localStorage,commonUtil,wfAlert,cryptUtil,commonConstant,$http, $uibModalInstance, items) ->
    $scope.type=type=items.type
    $scope.data=items.data
    $scope.modalTitle=if type=='new' then '新建账号' else '编辑账号'
    $scope.sexList=commonConstant.sexList()
    $scope.modalEnabledList=commonConstant.enabledList()
    #    qgy start
    $scope.apartmentList=[]
    loadApartment=()->
        list=[{id:undefined,name:'全部'}]
        $http.get('/apartment',params:{page:0,size:999})
        .success (resp)->
            if resp.code==0
                list=list.concat(resp.message.content)
                $scope.apartmentList=list
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadApartment()
    #    qgy end
    $scope.newData=()->
        postData={
            accountId:$localStorage['accountId']
            username:$scope.data.username
            nickname:$scope.data.nickname
            sex:$scope.data.sex
            store:$scope.data.store
            enabled:$scope.data.enabled
            email:$scope.data.email
            phone:$scope.data.phone
            buzCode:$scope.data.buzCode
            org:$scope.data.org
        }
        console.log('$scope.data.password:',$scope.data.password)
        if $scope.data.password
            postData.password=cryptUtil.hex_md5($scope.data.password)
        $http.post('@ostengar/adminUser', postData)
        .success((resp)->
            if resp.code == 0
                wfAlert.toastInfo('创建用户成功')
                $uibModalInstance.close('done')
            else
                wfAlert.toastError(resp.message)
        )

    $scope.editData=()->
        postData={
            accountId:$localStorage['accountId']
            username:$scope.data.username
            nickname:$scope.data.nickname
            sex:$scope.data.sex
            enabled:$scope.data.enabled
            email:$scope.data.email
            phone:$scope.data.phone
            buzCode:$scope.data.buzCode
            org:$scope.data.org
        }
        console.log('$scope.data.password:',$scope.data.password)
        if $scope.data.password
            postData.password=cryptUtil.hex_md5($scope.data.password)
        $http.put('/adminUser/@ostengar'+$scope.data.id,postData)
        .success((resp)->
            if(resp.code==0)
                wfAlert.toastInfo('编辑用户成功')
                $uibModalInstance.close('done')
            else
                wfAlert.toastError('编辑用户失败：'+resp.message)
        )
    $scope.judge=()->
        if type=='new'
            false
        else
            true
    $scope.confirm = ()->
        if type=='new'
            $scope.newData()
        else
            $scope.editData()
    $scope.cancel =()->
        $uibModalInstance.dismiss('cancel')

)